﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Security.Permissions;
namespace WPFExcelReport
{
    public class Watcher
    {
        string file = "";
        ITXPrint print = null;
        Queue<string> jobQ = null;
        string printorfile = "";
        public Watcher(List<string> watchpaths,string printorfile)
        {
    
            this.printorfile = printorfile;
            foreach (string watchpath in watchpaths)
            {
                if (System.IO.Directory.Exists(watchpath))
                {
                    Run(watchpath);
                }
            } 
        }
        [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
        public void Run(string path)
        {          
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = path;
            /* Watch for changes in LastAccess and LastWrite times, and
               the renaming of files or directories. */
            watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
               | NotifyFilters.FileName | NotifyFilters.DirectoryName;
            // Only watch text files.
            //watcher.Filter = "*.txt";
            watcher.Filter = "*.*";

            // Add event handlers.
            //watcher.Changed += new FileSystemEventHandler(OnChanged);
            watcher.Created += new FileSystemEventHandler(OnChanged);
            //watcher.Deleted += new FileSystemEventHandler(OnChanged);
            //watcher.Renamed += new RenamedEventHandler(OnRenamed);

            // Begin watching.
            watcher.EnableRaisingEvents = true;
            // Wait for the user to quit the program.
            //Console.WriteLine("Press \'q\' to quit the sample.");
            //while (Console.Read() != 'q') ;
        }

        // Define the event handlers. 
        private  void OnChanged(object source, FileSystemEventArgs e)
        {
            try
            {
                // Specify what is done when a file is changed, created, or deleted.
                string file = e.FullPath;
                if (!file.EndsWith("ITXTTF"))
                {
                    return;
                }
                if (print == null)
                {
                    print = new ITXPrint();
                    print.printorfile = printorfile;
                }
                if (jobQ == null)
                    jobQ = new Queue<string>();

                jobQ.Enqueue(file);
                ExecutePrint();
            }
            catch (Exception ex)
            { 
              
            }
        }
        private void ExecutePrint()
        {
            lock (jobQ)
            {
                while (jobQ.Count > 0)
                {
                    string job = jobQ.Dequeue();
                    print.ExecutePrint(job);
                }
            }
        }

        private void OnRenamed(object source, RenamedEventArgs e)
        {
            // Specify what is done when a file is renamed.
            Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
        }
    }
}